<?php

include_once '../inc/db_util.php';

function viewAllDosen() {
    $array_dosen = array();
    $query = mysql_query("SELECT * FROM tb_dosen ORDER BY nip");

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Nip'] = $row['nip'];
        $row_array['Nama'] = $row['namadosen'];
        $row_array['Username'] = $row['username'];
        $row_array['Alamat'] = $row['alamat'];
        $row_array['Telepon'] = $row['telepon'];
        $row_array['Email'] = $row['email'];

        array_push($array_dosen, $row_array);
    }
    return json_encode($array_dosen);
}

function viewDosenAccount($json) {
    $array_dosen = array();

    $jsArray = json_decode($json, false);
    $query = mysql_query("SELECT * FROM tb_dosen WHERE nip='" . $jsArray->{'Nip'} . "'");

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Nip'] = $row['nip'];
        $row_array['Nama'] = $row['namadosen'];
        $row_array['Username'] = $row['username'];
        $row_array['Alamat'] = $row['alamat'];
        $row_array['Telepon'] = $row['telepon'];
        $row_array['Email'] = $row['email'];

        array_push($array_dosen, $row_array);
    }
    return json_encode($array_dosen);
}

function updateDosenAccount($json) {
    $jsArray = json_decode($json, false);
    $query = "UPDATE tb_dosen SET 
        nip='" . $jsArray->{'Nip'} . "',
        username='" . $jsArray->{'Username'} . "',
        password='" . md5($jsArray->{'Password'}) . "',
        namadosen='" . $jsArray->{'Nama'} . "',
        alamat='" . $jsArray->{'Alamat'} . "',
        telepon='" . $jsArray->{'Telepon'} . "',
        email='" . $jsArray->{'Email'} . "'
        WHERE nip='" . $jsArray->{'Id'} . "'";

    return mysql_query($query);
}

function updateWebAccount($nip, $username, $oldpass, $newpass) {
    $result = mysql_query("SELECT nip FROM tb_dosen WHERE nip='$nip' AND password=MD5('$oldpass')") or die(mysql_error());
    $row = mysql_num_rows($result) or die(mysql_error());

    if ($row > 0) {
        $query = "UPDATE tb_dosen SET username='" . $username . "', password='" . md5($newpass) . "' WHERE nip='" . $nip . "'";
        $result = mysql_query($query) or die(mysql_error());
        if ($result) {
            return "Perubahan telah di simpan $row";
        } else {
            return "Gagal menyimpan perubahan!";
        }
    } else {
        return "Password lama tidak cocok! $row";
    }
}

function loginDosen($nip, $username, $password) {
    $array_dosen = array();
    $qLoginNoName = "SELECT nip, namadosen, username, admin FROM tb_dosen WHERE nip='" . $nip . "' AND password='" . md5($password) . "' AND admin != '1'";
	$qLoginWithName = "SELECT nip, namadosen, username, admin FROM tb_dosen WHERE nip='" . $nip . "' AND username='" . $username . "' AND password='" . md5($password) . "' AND admin != '1'";
    $queryLogin = $username == NULL ? $qLoginNoName : $qLoginWithName;
	
	$query = mysql_query($queryLogin);

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['LoginUser']['Id'] = $row['nip'];
        $row_array['LoginUser']['IdUser'] = $row['nip'];
        $row_array['LoginUser']['Nama'] = $row['namadosen'];
        $row_array['LoginUser']['Username'] = $row['username'];
        //$row_array['LoginUser']['Type'] = $row['admin']=='1' ? 'Admin' : 'Dosen';
        $row_array['LoginUser']['Type'] = 'Dosen';
        $row_array['Status'] = 'Success';

        array_push($array_dosen, $row_array);
    }

    $result = mysql_num_rows($query);

    if ($result > 0) {
        return json_encode($array_dosen);
    } else {
        $row_array['Status'] = 'Login Failed!';
        $row_array['LoginUser'] = NULL;

        array_push($array_dosen, $row_array);
        return json_encode($array_dosen);
    }
}

function loginWebAdmin($nip, $password) {
    $array_dosen = array();
    $query = mysql_query("SELECT nip, username, namadosen, admin FROM tb_dosen WHERE nip='" . $nip . "' AND password='" . md5($password) . "' AND admin='1'");
    $row = mysql_fetch_assoc($query, MYSQL_BOTH);

    $result = mysql_num_rows($query);
    if ($result > 0) {
        $row_array['Nip'] = $row['nip'];
        $row_array['Username'] = $row['username'];
        $row_array['Name'] = $row['namadosen'];
        $row_array['Status'] = 'Success';

        session_start();
        $_SESSION['nip'] = $row['nip'];
        $_SESSION['username'] = $row['username'];
        $_SESSION['admin'] = true;

        array_push($array_dosen, $row_array);
        return json_encode($array_dosen);
    } else {
        $row_array['Status'] = 'Login failed! NIP/Password not found...';
        array_push($array_dosen, $row_array);
        return json_encode($array_dosen);
    }
}

function approvePendaftaranByNim($nim) {
    $query = mysql_query("INSERT INTO tb_mahasiswa(id_mahasiswa, nim, nama, username, `password`, alamat, telepon, email)
            SELECT t.id_mahasiswa, t.nim, t.nama, t.username, t.`password`, t.alamat, t.telepon, t.email FROM tb_temp_mahasiswa t WHERE t.nim='" . $nim . "'");
    if ($query)
        $query = mysql_query("DELETE FROM tb_temp_mahasiswa WHERE nim='" . $nim . "'");
    if ($query)
        return true;
    return false;
}

function approveAllPendaftaran() {
    $query = mysql_query("INSERT INTO tb_mahasiswa(id_mahasiswa, nim, nama, username, `password`, alamat, telepon, email)
            SELECT t.id_mahasiswa, t.nim, t.nama, t.username, t.`password`, t.alamat, t.telepon, t.email FROM tb_temp_mahasiswa t");
    if ($query)
        $query = mysql_query("TRUNCATE tb_temp_mahasiswa");
    if ($query)
        return true;
    else
        return false;
}

function rejectPendaftaranByNim($nim) {
    $query = mysql_query("DELETE FROM tb_temp_mahasiswa WHERE nim='" . $nim . "'");
    if ($query)
        return true;
    return false;
}

?>
